package com.gl.mobile.activities;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import com.gl.mobile.R;
import com.gl.mobile.constants.Constants;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class DetalleLibroActivity extends Activity {
    /** Called when the activity is first created. */
	
	private TextView titleTV;
	
	private TextView detalleTV;
	
	private RatingBar ratingBar;
	
	private ImageView detalleImagen;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detalle_libro);
        
        Bundle bundle = getIntent().getExtras();
        
        titleTV = (TextView) findViewById( R.id.detalle_title );
        detalleTV = (TextView) findViewById( R.id.detalle_autor );
        ratingBar = (RatingBar) findViewById( R.id.detalle_ratingBar );
        detalleImagen = (ImageView) findViewById( R.id.detalle_imagen );
        
        if( bundle.getString("imagen") != "" )
        {
			try {
				URL url;
				
				url = new URL( Constants.BASE_PATH + "/imagenes/" + bundle.getString("imagen") );
				URLConnection conn = url.openConnection();
	            conn.connect();

	            InputStream is = conn.getInputStream();
	            BufferedInputStream bis = new BufferedInputStream(is);
	            Bitmap bm = BitmapFactory.decodeStream(bis);

	            bis.close();
	            is.close();
	            detalleImagen.setImageBitmap(bm);

			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
           
        }

        
        titleTV.setText( bundle.getString("nombre") );
        detalleTV.setText( bundle.getString("autor") + "\n" + bundle.getString("editorial") + "\n" + bundle.getString("idioma") );
        
        
        ratingBar.setRating( new Float(bundle.getString("puntuacion")) );
        ratingBar.setEnabled(false);
        
    }
    
}